# -*- coding:utf-8 -*-
from models.CoCode import CoCode
import uniout

print 'Hello', 'SQLAlchemy(no session)', '!'

from sqlalchemy import create_engine
import setting

DB_CONNECT_STRING = 'mysql://airflow:password@172.17.0.2:3306/airflowdb?charset=utf8'
engine = create_engine(DB_CONNECT_STRING, echo=setting.echo)
conn = engine.connect()

tb = CoCode.__table__

sql = tb.select(tb.c.CName == '天气')
print sql

r = conn.execute(sql).fetchall()
print r                             # [(242L, u'天气', u'晴', u'qing', 1L, 1L)]
# print r.__name__
print r.__class__                   # <type 'list'>
print r.__doc__                     #list() -> new empty list \list(iterable) -> new list initialized from iterable's items
# print r.__dict__
# print r.__bases__
#print r.__module__


# all = conn.execute(tb.select()).fetchall()
allcode = conn.execute("select * from Co_Code").fetchall()
print filter(lambda x: x.CName == u'天气', allcode)
print reduce(lambda x, y: x if x.CSort > y.CSort else y, allcode)
a = map(lambda x: x.Status == 1, allcode)
print a
allEnable = reduce(lambda x, y: x == y and x == True, a)
print allEnable

def func_all(f, lst):
    a = map(f, lst)
    return reduce(lambda x, y: x == y and x == True, a)

print func_all(lambda x: x.CodeId > 0, allcode)

def func_all(f, lst):
    a = map(f, lst)
    return all(a)

print func_all(lambda x: x.CodeId > 1, allcode)


def func_any(f, lst):
    a = map(f, lst)
    return any(a)

print func_any(lambda x: x.CName == u'天气', allcode)


'''
from models.dw_models import DtXdLnFinal, Base
#from sqlalchemy import engine

#Base.metadata.create_all(engine, [DtXdLnFinal])    # 不对
print DtXdLnFinal.__table__.create(engine, checkfirst=True)  # 直接执行了;没有, checkfirst=True会报异常
'''